//
//  XMGTabBarController.m
//  百思不得解国内
//
//  Created by sunnyday on 16/5/15.
//  Copyright © 2016年 sunnyday. All rights reserved.
//

#import "XMGTabBarController.h"
#import "XMGEssenceViewController.h"
#import "XMGNewViewController.h"
#import "XMGFriendTrendsViewController.h"
#import "XMGMeViewController.h"
#import "XMGTabBar.h"


#import "XMGNavigationController.h"


@interface XMGTabBarController ()

@end

@implementation XMGTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    /**
     *  tabbarcontroller  里的内容
     */
// 创建子 控制器
    
    /**
     
     UI_APPEARANCE_SELECTOR  又这个宏的 都可以用一种方法 appearance 统一设置 所有uitabbaritem 的文字的属性
     */
    
    //字号 settitletextattributes     属性中 的设置字体的属性 得用到字典
    
    
    [UINavigationBar appearance];
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    //set color
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
   
    
    
    //选中的字的状态
    NSMutableDictionary *selectedattrs = [NSMutableDictionary dictionary];
    selectedattrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedattrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedattrs forState:UIControlStateSelected];

    
    
    
    
    [self setUpChildVc:[[XMGEssenceViewController alloc]init] title:@"精选" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    
    [self setUpChildVc:[[XMGNewViewController alloc]init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];

    
    [self setUpChildVc:[[XMGFriendTrendsViewController alloc]init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];

    
    [self setUpChildVc:[[XMGMeViewController alloc]init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];

    
    
    // 自己重写  tabbar
    // 因为是只读  所以 用kvc
    [self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];
    
}

/**
 *  封装
 */
-(void)setUpChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置文字和图片
    vc.tabBarItem.title = title;
    //vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0];
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    //创建一个导航控制器 添加导航控制器 为 tabbarcontroller 的 子控制器   各个页面的控制器 再成为 tabbarcontroller的子控制器
    
    
    // 自定义 NavigationController
    XMGNavigationController *nav = [[XMGNavigationController alloc] initWithRootViewController:vc];
    // 添加子控制器
    
    [self addChildViewController:nav];
    [nav.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"]];

}
@end
